<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/09/13
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Driver;
use think\Db;
use think\Config;



/**
 * 加油站🛢
 */
class Pfs extends Driver
{

    protected $noNeedLogin = ['p'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
        // $user = $this->auth->getUserinfo();

        // if($user->group_id==1){
        //     $this->error('普通用户！非司机');
        // }
        
    }

    /**
     * 注册加油站🛢
     *
     * @param string $name 加油站名称
     * @param string $lng 经度
     * @param string $lat 纬度
     * @param string $addr 详细地址
     * @param string $tel 联系电话
     * @param string $contacts 联系人
     * @param string $logo_image 加油站logo图片
     * @param string $images banner图
     * @return void
     */
    public function reg_pfs($name,$lng,$lat,$addr,$tel,$contacts,$logo_image,$images)
    {
      
        $data['uid'] = $this->auth->id;
        $pfs = Db::name('pfs')->where($data)->find();
        if($pfs){
            Db::name('user')->where('id',$this->auth->id)->update(['rule_status'=>'1']);
            $this->error('您已注册加油站');
        }
        //判断是否有其他的店铺角色
        if($this->auth->rule_status>0){
            $this->error('您已有其他的店铺');
        }

        $data['name'] = $name;
        $data['lng'] = $lng;
        $data['lat'] = $lat;
        $data['addr'] = $addr;

        $data['tel'] = $tel;
        $data['contacts'] = $contacts;
        $data['logo_image'] = $logo_image;
        $data['images'] = $images;

        $data['switch'] = 1;//状态:0=不显示,1=正常显示
        $data['createtime'] = time();

        $id = Db::name('pfs')->insertGetId($data);
        if($id){
            $this->UpdateGeoPfs($lng,$lat,$id);//更新加油站定位
            //修改附属角色
            Db::name('user')->where('id',$this->auth->id)->update(['rule_status'=>'1']);
            $this->success('注册成功');
        }

    }

    

    /**
     * 编辑加油站信息🛢
     *
     * @param string $name 加油站名称
     * @param string $lng 经度
     * @param string $lat 纬度
     * @param string $addr 详细地址
     * @param string $tel 联系电话
     * @param string $contacts 联系人
     * @param string $logo_image 加油站logo图片
     * @param string $images banner图
     * @return void
     */
    public function edit_pfs($name = null ,$lng = null ,$lat = null ,$addr = null ,$tel = null ,$contacts = null ,$logo_image = null ,$images = null )
    {
        $where['uid'] = $this->auth->id;
        $pfs = Db::name('pfs')->where($where)->find();
        if(!$pfs){
            $this->error('您没有加油站');
        }elseif($pfs['switch']!=1){
            $this->error('您的加油站已被关闭，请联系平台');
        }

        $data['name'] = $name;
        $data['lng'] = $lng;
        $data['lat'] = $lat;
        $data['addr'] = $addr;

        $data['tel'] = $tel;
        $data['contacts'] = $contacts;
        $data['logo_image'] = $logo_image;
        $data['images'] = $images;

        // $data['switch'] = 1;//状态:0=不显示,1=正常显示
        $data['updatetime'] = time();

        $res = Db::name('pfs')->where($where)->update($data);
        if($res){
            $this->UpdateGeoPfs($lng,$lat,$pfs['id']);//更新加油站定位
            $this->success('修改成功');
        }else{
            $this->error('未修改内容');
        }


    }



    /**
     * 获取我的加油站信息🛢
     *
     * @return void
     */
    public function get_my_pfs_data()
    {
     
        $pfs = $this->GetPfsDataByUid($this->auth->id);
        // if(!$pfs){
        //     $this->error('您没有加油站');
        // }
        $this->success('ok',$pfs);
    }



    /**
     * 编辑油价价格🛢
     * @ApiSummary  (json格式【[oil_id油型号id，price原价,new_price今日价格]，n个】)
     * @param string $json
     * @return void
     */
    public function edit_pfs_price($json=null)
    {
        //[{ "oil_id": 1, "price": 8, "new_price": 7},{ "oil_id": 2, "price": 9, "new_price": 6 }]
        $pfs = $this->GetPfsDataByUid($this->auth->id);
        $list = htmlspecialchars_decode($json);
        $list = json_decode($list,true);
        // halt($list);
        $ci = 0;
        foreach ($list as $key => $val) {
            $temp['pfs_id'] = $pfs['id'];
            $temp['oil_id'] = $val['oil_id'];
            $temp['price']  = $val['price'];
            $temp['new_price']  = $val['new_price'];
            $res = $this->update_price($temp);//修改油价
            if($res)$ci++;

        }
        $this->success('编辑成功',$ci);
    }

    /**
     * 修改价格🛢
     *
     * @param array $data
     * @return void
     */
    protected function update_price($data)
    {
        $res = Db::name('pfs_price')->where('pfs_id',$data['pfs_id'])->where('oil_id',$data['oil_id'])->find();
        if($res){
            $res = Db::name('pfs_price')->where('pfs_id',$data['pfs_id'])->where('oil_id',$data['oil_id'])->update($data);
        }else{
            $res = Db::name('pfs_price')->insert($data);
        }
        return $res;
    }


}